//
//  NetManager.m
//  查询工具
//
//  Created by student on 16/3/29.
//  Copyright © 2016年 wk. All rights reserved.
//

#import "NetManager.h"

@implementation NetManager
/**
 *  查询身份证
 *
 *
 *
 *
 */
+(void)requestInfoByPersonID:(NSString *)personID
                successBlock:(void(^)(NSString *))successBlock
                   failBlock:(void(^)(NSError *))failBlock{
    NSString *httpUrl = @"http://apis.baidu.com/apistore/idservice/id";
    NSString *httpArg = [NSString stringWithFormat:@"id=%@",personID];
    NSString *urlStr = [NSString stringWithFormat:@"%@?%@",httpUrl,httpArg];
    NSURL *url = [NSURL URLWithString: urlStr];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
    [request setHTTPMethod: @"GET"];
    [request addValue: @"027c141dc416ca3d5165488de2102049" forHTTPHeaderField: @"apikey"];
    //NSError *error;
    [NSURLConnection sendAsynchronousRequest: request
                                       queue: [NSOperationQueue mainQueue]
                           completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
                                if (error == nil)
                                   {
                                   NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
                                   NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                   NSLog(@"HttpResponseCode:%ld", responseCode);
                                   NSLog(@"HttpResponseBody %@",responseString);
                                   NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
                                       if ([dict[@"errNum"] isEqualToString:@"0"]) {
                                           NSDictionary *dict1 = dict[@"retData"];
                                           
                                           NSString *result = [NSString stringWithFormat:@"生日:%@\n地址:%@\n性别:%@",dict1[@"birthday"],dict1[@"address"],dict1[@"sex"]];
                                           successBlock(result);

                                       }
                                       else{
                                           failBlock(error);
                                       }
                                                                   }
                               else {
                                   NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code);
                                   failBlock(error);
                               }
                           }];
    
}

@end
